package com.xxwy.ddu.common.autowired.config;


import com.xxwy.ddu.common.filter.LogFilter;
import com.xxwy.ddu.common.interceptor.LogInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.*;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;


/**
 * @author ShenHuaJie
 * @since 2018年5月10日 下午2:45:52
 */
@Configuration
public  class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**");
    }

    @Bean
    public FilterRegistrationBean<CharacterEncodingFilter> encodingFilterRegistration() {
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding("UTF-8");
        encodingFilter.setForceEncoding(true);
        FilterRegistrationBean<CharacterEncodingFilter> registration = new FilterRegistrationBean<CharacterEncodingFilter>(
                encodingFilter);
        registration.setName("encodingFilter");
        registration.addUrlPatterns("/*");
        registration.setAsyncSupported(true);
        registration.setOrder(1);
        return registration;
    }

    /**
     *过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean<LogFilter> localeFilterRegistration() {
        FilterRegistrationBean<LogFilter> registration = new FilterRegistrationBean<LogFilter>(
                new LogFilter());
        registration.setName("logFilter");
        registration.addUrlPatterns("/*");
        //优先级
        registration.setOrder(Integer.MAX_VALUE);
        return registration;
    }



//    @Bean
//    public InternalResourceViewResolver viewResolver() {
//        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
//        viewResolver.setPrefix("/WEB-INF/jsp/");
//        viewResolver.setSuffix(".jsp");
//        viewResolver.setViewClass(JstlView.class);
//        return viewResolver;
//    }



//    @Override
//    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//        configurer.enable();
//    }

//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//        List<MediaType> mediaTypes = InstanceUtil.newArrayList();
//        mediaTypes.add(MediaType.valueOf("application/json;charset=UTF-8"));
//        mediaTypes.add(MediaType.valueOf("text/html"));
//        converter.setSupportedMediaTypes(mediaTypes);
//        converter.setFeatures(SerializerFeature.QuoteFieldNames, SerializerFeature.WriteDateUseDateFormat,
//            SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNonStringValueAsString);
//        converters.add(converter);
//    }
//
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        MaliciousRequestInterceptor requestInterceptor = new MaliciousRequestInterceptor();
//        if (DataUtil.isNotEmpty(PropertiesUtil.getInt("request.minInterval"))) {
//            requestInterceptor.setMinRequestIntervalTime(PropertiesUtil.getInt("request.minInterval"));
//        }
//        requestInterceptor.setNextInterceptor(eventInterceptor());
//        registry.addInterceptor(requestInterceptor).addPathPatterns("/**").excludePathPatterns("/*.ico", "/*/api-docs",
//            "/swagger**", "/swagger-resources/**", "/webjars/**", "/configuration/**");
//    }


//    // 资源重定向(仅作为后台使用不提供静态资源)
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("upload/**").addResourceLocations("/WEB-INF/upload/");
//        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
//        registry.addResourceHandler("webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
//    }

//    // 允许跨域的接口
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/api/*").allowedOrigins("*").allowCredentials(false)
//        .allowedMethods("GET", "POST", "DELETE", "PUT")
//        .allowedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Headers",
//            "Access-Control-Allow-Methods", "Access-Control-Max-Age")
//        .exposedHeaders("Access-Control-Allow-Origin").maxAge(3600);
//    }
}
